// Package controllers
// @Title  articleCat.go
// @Description  文章目录
// @Author  小斌 (时间 格式是2021/12/02  11:40)
// @Update  小斌 (时间 格式是2021/12/02  11:40)

package controllers

import (
	"beego-admin/models"
	"time"
)

type ArticleCatController struct {
	BaseController
}

func (this *ArticleCatController) Index() {
	this.TplName = "admin/article/article_cat_list.tpl"
}

func (this *ArticleCatController) ArticleCatAdd() {
	this.TplName = "admin/article/article_cat_add.tpl"
}

func (this *ArticleCatController) ArticleCatEdit() {
	id,_ := this.GetInt("id",0)
	articleCatInfo,_ := models.GetArticleCatInfoById(id)

	this.Data["article_cat_info"] = articleCatInfo
	this.TplName = "admin/article/article_cat_edit.tpl"
}

func (this *ArticleCatController) GetArticleCatList(){
	page,_ := this.GetInt("page",1)
	pageSize,_ := this.GetInt("limit",5)
	list, count, _ := models.GetArticleCatList(pageSize,page)

	type CarouseObj struct{
		Id                 int    `json:"id"`
		CatName				string `json:"cat_name"`
		Status               int `json:"status"`
		StatusName               string `json:"status_name"`
		UpdateTime                time.Time `json:"update_time"`
		AddTime                time.Time `json:"add_time"`
	}
	var returnData[]CarouseObj
	for _, v := range *list {
		statusName := "启用"
		if v.Status != 0{
			statusName = "停用"
		}

		tmpParam := CarouseObj{v.Id,v.CatName,v.Status,statusName,v.UpdateTime,v.AddTime}
		returnData = append(returnData, tmpParam)
	}


	this.Data["json"] = this.AjaxLayuiTableReturn(0,"成功",count,returnData)
	this.ServeJSON()
}


func (this *ArticleCatController) AjaxAddArticleCat() {
	catName := this.GetString("cat_name")
	status,_ := this.GetInt("status",0)
	if catName == ""{
		this.Data["json"] = this.AjaxReturn(10001,"目录名称不能为空",nil)
		this.ServeJSON()
		this.StopRun()
	}

	id := models.AddArticleCat(catName,status)
	if id > 0{
		this.Data["json"] = this.AjaxReturn(10000,"添加成功",nil)
	}else{
		this.Data["json"] = this.AjaxReturn(10001,"添加失败",nil)
	}

	this.ServeJSON()
}

func (this *ArticleCatController) AjaxEditArticleCat() {
	articleCatId,_ := this.GetInt("id",0)
	catName := this.GetString("cat_name")
	status,_ := this.GetInt("status",0)
	if catName == ""{
		this.Data["json"] = this.AjaxReturn(10001,"目录名称不能为空",nil)
		this.ServeJSON()
		this.StopRun()
	}

	id := models.UpdateArticleCatById(articleCatId,catName,status)
	if id > 0{
		this.Data["json"] = this.AjaxReturn(10000,"修改成功",nil)
	}else{
		this.Data["json"] = this.AjaxReturn(10001,"修改失败",nil)
	}

	this.ServeJSON()
}

// AjaxDeleteArticleCat ajax删除图片
func (this *ArticleCatController) AjaxDeleteArticleCat() {
	articleCatId,_ := this.GetInt("id",0)

	adminUserInfo,_ := models.GetArticleCatInfoById(articleCatId)
	if adminUserInfo.Id <= 0{
		this.Data["json"] = this.AjaxReturn(10000,"删除的记录不存在",nil)
		this.ServeJSON()
		this.StopRun()
	}
	id := models.DeleteArticleCatById(articleCatId)
	if id > 0{
		this.Data["json"] = this.AjaxReturn(10000,"删除成功",nil)
	}else{
		this.Data["json"] = this.AjaxReturn(10001,"删除失败",nil)
	}

	this.ServeJSON()
}